package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.*;
import com.ws.ssmzongjie3.service.CollectionsService;
import com.ws.ssmzongjie3.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/collection")
public class CollectionController {
    @Autowired
    private CollectionsService collectionService;
    @Autowired
    private RouteService routeService;
    //收藏
    @RequestMapping("/list")
    public String list(Model model, ProVo proVo, Route route, HttpSession session){
        Integer userId = (Integer) session.getAttribute("userId");
        System.out.println("links:"+route);
        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        List<Route> collections = routeService.listRouteWithLikes(route,userId,true);
        PageInfo pageInfo = new PageInfo(collections);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("user",route);

        return "/WEB-INF/admin/collections/list";
    }
    @RequestMapping("/qianlist")
    public String select(Model model, ProVo proVo, Route route, HttpSession session){
        User user = (User) session.getAttribute("user");
        System.out.println("links:"+route);
        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        List<Route> collections = routeService.listRouteWithCollections(route,user.getUser_id(),true);
        PageInfo pageInfo = new PageInfo(collections);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("route",route);

        return "/WEB-INF/qiantai/collections/list";
    }
   
    @RequestMapping(value = "/shoucang", method = RequestMethod.POST) // 明确指定POST请求
    @ResponseBody
    public Map<String, Object> insert(@RequestParam("route_id") Integer routeId, @RequestParam("user_id") Integer userId) {
        Map<String, Object> result = new HashMap<>();
        // 检查用户是否登录
        if (userId == null) {
            result.put("success", false);
            result.put("message", "请先登录！");
            return result;
        }

        Collections collection = new Collections();

        collection.setRoute_id(routeId);
        collection.setUser_id(userId);
        // 检查是否已收藏
        Collections collectionById = collectionService.getCollectionById(collection);
        System.out.println(collectionById+"============================");
        if (collectionById != null) {
            // 如果已收藏，则取消收藏
            int i = collectionService.deleteCollection(collection);
            if (i == 1) {
                result.put("success", true);
                result.put("message", "取消收藏成功！");
            } else {
                result.put("success", false);
                result.put("message", "取消收藏失败，请重试！");
            }
        } else {
            // 如果未收藏，则插入收藏记录
            int i = collectionService.insertCollection(collection);
            if (i == 1) {
                result.put("success", true);
                result.put("message", "收藏成功！");
            } else {
                result.put("success", false);
                result.put("message", "收藏失败，请重试！");
            }
        }

        // 获取收藏数量并添加到返回结果中
        int collectCount = collectionService.countCollectionByRouteId(routeId);
        result.put("collectCount", collectCount);

        return result;
    }
    @RequestMapping("/delete")
    public String delete(Collections collection) {
        collectionService.deleteCollection(collection);
        return "redirect:/collection/qianlist";
    }
    @RequestMapping("/huixian")
    public String huixian() {
        return "qiancollection";
    }
    @RequestMapping("/update")
    public String update() {
        return "qiancollection";
    }
    @RequestMapping("/select")
    public String select() {
        return "qiancollection";
    }

    @RequestMapping("/paihang")
    public String rank(Model model, ProVo proVo, Route route, HttpSession session){
        Integer userId = (Integer) session.getAttribute("userId");
        System.out.println("links:"+route);
        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        List<Route> collections = routeService.collectionDesc(route);
        PageInfo pageInfo = new PageInfo(collections);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("route",route);
        return "/WEB-INF/qiantai/favoriterank";
    }

}
